<?php


namespace app\admin\services;


use app\admin\model\SkuCategoryValueModel;
use think\facade\Db;
use think\facade\Validate;

class SkuCategoryValueService extends BaseService
{
    public static function tableList($p)
    {

        $params = [
            'where' => [],
            'order' => 'id desc',
            'page'=>($p['pageNo'])??1,
            'pageSize'=>($p['pageSize'])??10,
            'with' => [
                'creator' => function ($query) {
                    $query->withField(['id','nickname'])->withDefault(['id'=>'','nickname'=>'管理员已删除']);
                },
            ],
            'field'=>'id,name,creator_id,updater_id,object,code,type,input_type,production_attribute'

        ];
        if (!empty($p['keyword']))
            $params['where'][] = ['name|code', 'like', '%'.$p['keyword'].'%'];
        if (!empty($p['production_attribute']))
            $params['where'][] = ['production_attribute', '=', $p['production_attribute']-1];
        $list = SkuCategoryValueModel::getList($params);
        foreach ($list as $item){
            $item['id']=(string)$item['id'];
//            $item['object']=json_encode($item['object']);
        }
        $count = SkuCategoryValueModel::count($params['where']);
        api_return(200, '', ['list' => $list, 'total' => $count]);
    }

    public static function edit($params)
    {

        $rule = [
            'name' => 'require',
            'code'=>'require',
//            'object'=>'require'
        ];
        $validate=Validate::rule($rule);
        if(!$validate->check($params))
            api_return(500,$validate->getError());
        SkuCategoryValueModel::duplicateCheck('name',$params);
//        Db::startTrans();
//        $content=json_decode(json_encode($params['content']),true);
//        $editList=[];
//        foreach ($content as $key=>$p){
//            if(is_array($p['object']))
//                $p['object']=json_encode($p['object']);
//            if (empty($p['id']))
//                $p['creator_id'] = $params['user_id'];
//            $p['updater_id'] = $params['user_id'];
//            $p['sku_category_id']=$params['sku_category_id'];
//            $validate =Validate::rule($rule);
//            if (!$validate->check($p)) {
//                Db::rollback();
//                api_return(500, '第'.$key.'-'.$validate->getError());
//            }
//            $res=SkuCategoryValueModel::editWithReturn($p);
//            if(!$res){
//                Db::rollback();
//                api_return(500,'rollback soon');
//            }
//            if(!empty($p['id'])){
//                $editList[]=$p['id'];
//            }else{
//                $editList[]=$res;
//            }
//        }
//        if($editList)
//            SkuCategoryValueModel::where([
//                ['sku_category_id','=',$params['sku_category_id']],
//                ['id','not in',$editList]
//            ])->delete();
//        Db::commit();
//        api_return(200);
        SkuCategoryValueModel::edit($params);
    }

    public static function del($p)
    {
        $rule = ['ids' => 'require'];
        $validate = \think\facade\Validate::rule($rule);
        if (!$validate->check($p))
            api_return(500, $validate->getError());
        SkuCategoryValueModel::delByIds($p['ids']);

    }
}
